package com.insigma.business.qrcode.soldier._provider.impl;

import com.insigma.business.qrcode.common.converter.SoldierDisabledInfoProviderConverter;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier._provider.SoldierDisabledInfoProvider;
import com.insigma.business.qrcode.soldier.dto.SoldierDisabledInfoDTO;
import com.insigma.business.qrcode.soldier.dto.request.SoldierDisabledInfoInsertRequest;
import com.insigma.business.qrcode.soldier.dto.request.SoldierDisabledInfoSearchRequest;
import com.insigma.business.qrcode.soldier.dto.request.SoldierDisabledInfoUpdateRequest;
import com.insigma.business.qrcode.soldier.dto.searcher.SoldierDisabledInfoBSearcher;
import com.insigma.business.qrcode.soldier.entity.SoldierDisabledInfoDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.service.SoldierDisabledInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("soldierDisabledInfoProvider")
public class SoldierDisabledInfoProviderImpl extends BaseProviderImpl implements SoldierDisabledInfoProvider {

    @Autowired
    private SoldierDisabledInfoService soldierDisabledInfoService;


    @Override
    public BasePagingResponse<List<SoldierDisabledInfoDTO>> searchSoldierDisabledInfo(final SoldierDisabledInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<SoldierDisabledInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页SoldierDisabledInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final SoldierDisabledInfoBSearcher soldierDisabledInfoBSearcher = SoldierDisabledInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<SoldierDisabledInfoDO> soldierDisabledInfoDOList = soldierDisabledInfoService.search(soldierDisabledInfoBSearcher);
                final List<SoldierDisabledInfoDTO> soldierDisabledInfoDTOList = SoldierDisabledInfoProviderConverter.convertResponseDOList2DTOList(soldierDisabledInfoDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, soldierDisabledInfoDTOList, searchRequest,soldierDisabledInfoBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_DISABLED_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<SoldierDisabledInfoDTO> getSoldierDisabledInfo(final BaseGetRequest baseGetRequest){

        final BaseResponse<SoldierDisabledInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条SoldierDisabledInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final SoldierDisabledInfoDO soldierDisabledInfoDO = soldierDisabledInfoService.get((String)baseGetRequest.getId());
                final SoldierDisabledInfoDTO remoteObj = SoldierDisabledInfoProviderConverter.convertResponseDO2DTO(soldierDisabledInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_DISABLED_INFO_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<SoldierDisabledInfoDTO> getSoldierDisabledInfo(String idCardNo) {
        SoldierDisabledInfoDO soldierDisabledInfoByIdCardNo = soldierDisabledInfoService.getSoldierDisabledInfoByIdCardNo(idCardNo);
        SoldierDisabledInfoDTO soldierDisabledInfoDTO = SoldierDisabledInfoProviderConverter.convertResponseDO2DTO(soldierDisabledInfoByIdCardNo);
        return ResponseUtils.buildSuccessResponse(soldierDisabledInfoDTO);
    }


    @Override
    public BaseResponse<String> insertSoldierDisabledInfo(final SoldierDisabledInfoInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增SoldierDisabledInfo的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final SoldierDisabledInfoDO soldierDisabledInfoDO = SoldierDisabledInfoProviderConverter.convertRequestIO2DO(insertRequest);
                soldierDisabledInfoService.insert(soldierDisabledInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, soldierDisabledInfoDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_DISABLED_INFO_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateSoldierDisabledInfo(final SoldierDisabledInfoUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改SoldierDisabledInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                SoldierDisabledInfoDO soldierDisabledInfoDO = SoldierDisabledInfoProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = soldierDisabledInfoService.update(soldierDisabledInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_DISABLED_INFO_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteSoldierDisabledInfo(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除SoldierDisabledInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = soldierDisabledInfoService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_DISABLED_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
